CC=gcc
CFLAGS=-Wall -O2

BINARIES:=writemem copymem copymem_4kb
OBJECTS:=

# grep -c: count
# grep -o: only matching

AMIGA:=$(shell uname)
ifeq ($(AMIGA),AmigaOS)
	OS:=$AMIGA
	CPUINFO:=$(shell cpu | grep -o -c "440")
	CFLAGS+=-D__USE_INLINE__
	ifeq ($(CPUINFO),1)
		WRITEMEM_ASM_OBJ:=writemem_asm.o
		CFLAGS+=-DCPU_PPC440 -mcpu=440fp
	endif
else
	OS:=Linux
	ARCH:=$(shell cat /proc/cpuinfo | grep -o -c "^flags")
	ifeq ($(ARCH),0)
		ARCH:=powerpc
		ALTIVEC:=$(shell cat /proc/cpuinfo | grep -c "altivec")
		ifeq ($(ALTIVEC),1)
			CFLAGS+=-DUSE_ALTIVEC -maltivec
		endif
		CPUINFO:=$(shell cat /proc/cpuinfo | grep -c "^cpu\s*: 440")
		ifeq ($(CPUINFO),1)
			WRITEMEM_ASM_OBJ:=writemem_asm.o
			CFLAGS+=-DCPU_PPC440
		endif
		CPUINFO:=$(shell cat /proc/cpuinfo | grep -c "^cpu\s*: PPC970")
		ifeq ($(CPUINFO),1)
			CFLAGS+=-DCPU_PPC970
		endif
	else
		ARCH:=x86
	endif
endif

all: $(BINARIES)

writemem: writemem.o $(WRITEMEM_ASM_OBJ)
	$(CC) -o writemem writemem.o $(WRITEMEM_ASM_OBJ)

writemem.o: writemem.c
	$(CC) $(CFLAGS) -c writemem.c

writemem_asm.o: writemem_asm.S
	$(CC) $(CFLAGS) -c writemem_asm.S

copymem: copymem.o
	$(CC) -o copymem copymem.o

copymem.o: copymem.c
	$(CC) $(CFLAGS) -c copymem.c

copymem_4kb: copymem_4kb.o
	$(CC) -o copymem_4kb copymem_4kb.o

copymem_4kb.o: copymem_4kb.c
	$(CC) $(CFLAGS) -c copymem_4kb.c

clean:
	rm *.o writemem copymem copymem_4kb

